<template>
  <!-- 新增视频库 -->
  <el-dialog
    :title="'新建'+typeText+'库'"
    :visible.sync="addRegistoryModal"
    width="457px"
    :modal-append-to-body="false"
  >
    <el-form
      ref="ruleForm"
      :model="ruleForm"
      :rules="rules"
      label-position="top"
      class="demo-ruleForm"
    >
      <el-form-item :label="typeText+'库名称'" prop="name">
        <el-input v-model="ruleForm.name" autofocus class="white" />
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button
        size="small"
        @click="addRegistoryModal = false"
      >取 消</el-button>
      <el-button
        type="primary"
        size="small"
        @click="submitForm('ruleForm')"
      >确 定</el-button>
    </span>
  </el-dialog>
</template>
<script>
export default {
  props: {
    type: {
      type: String,
      default: 'product'
    }
  },
  data() {
    return {
      addRegistoryModal: false,
      ruleForm: {
        name: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入名称', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    typeText() {
      return this.type === 'product' ? '视频' : '素材'
    }
  },
  methods: {
    submitForm(formName = 'ruleForm') {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$emit('get', { ...this.ruleForm })
          this.addRegistoryModal = false
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    resetForm(formName = 'ruleForm') {
      this.$refs[formName].resetFields()
      this.addRegistoryModal = false
    },
    open() {
      this.$nextTick(() => {
        if (this.$refs['ruleForm']) {
          this.$refs['ruleForm'].resetFields()
        }
      })
      this.addRegistoryModal = true
    },
    close() {
      this.resetForm()
    }
  }
}
</script>
